package com.bestcem.xm.user.config.properties;

import com.bestcem.xm.user.config.properties.info.SampleOrgInfo;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * sample公司配置
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/8 15:12
 */
@Data
@Component
@ConfigurationProperties(prefix = "app.user.sample")
public class SampleProperties {

    private List<SampleOrgInfo> orgList = new ArrayList<>();

    private SampleOrgInfo defaultOrg;

    /**
     * 设置默认样例公司code
     *
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @version v1.0
     * @date 2022/3/10 14:45
     */
    @PostConstruct
    public void init() {
        for (SampleOrgInfo sampleOrgInfo : orgList) {
            if (sampleOrgInfo.getDefaultOrgCodeFalg() != null && sampleOrgInfo.getDefaultOrgCodeFalg()) {
                this.defaultOrg = sampleOrgInfo;
                break;
            }
        }
    }

    /**
     * 通过行业code获取样例公司信息
     *
     * @param industryCode
     * @return SampleOrgInfo
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @version v1.0
     * @date 2022/3/11 17:39
     */
    public SampleOrgInfo getByIndustryCode(String industryCode) {
        if (StringUtils.isNotBlank(industryCode)) {
            for (SampleOrgInfo sampleOrgInfo : orgList) {
                if (industryCode.equals(sampleOrgInfo.getIndustryCode())) {
                    return sampleOrgInfo;
                }
            }
        }

        return null;
    }

    /**
     * 通过行业code获取样例公司信息找不到则返回默认的
     *
     * @param industryCode 行业编码
     * @return com.bestcem.xm.user.config.properties.info.SampleOrgInfo
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/05/15 11:05
     */
    public SampleOrgInfo getByIndustryCodeAndDefault(String industryCode) {
        SampleOrgInfo sampleOrgInfo = getByIndustryCode(industryCode);
        return Objects.isNull(sampleOrgInfo) ? defaultOrg : sampleOrgInfo;
    }

}
